package com.hadluo.qq.server.mapper;

import com.hadluo.qq.server.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 用户Mapper接口
 * @author hadluo
 */
@Mapper
public interface UserMapper {
    /**
     * 根据ID查询用户
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    User selectById(Long id);

    /**
     * 根据QQ号查询用户
     */
    @Select("SELECT * FROM users WHERE qq_number = #{qqNumber}")
    User selectByQQNumber(String qqNumber);

    /**
     * 查询所有在线用户
     */
    @Select("SELECT * FROM users WHERE status = 1")
    List<User> selectOnlineUsers();

    /**
     * 插入新用户
     */
    @Insert("INSERT INTO users (qq_number, nickname, password, gender, avatar_url, signature, status, create_time, update_time) " +
            "VALUES (#{qqNumber}, #{nickname}, #{password}, #{gender}, #{avatarUrl}, #{signature}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 更新用户信息
     */
    @Update("UPDATE users SET nickname = #{nickname}, password = #{password}, gender = #{gender}, " +
            "avatar_url = #{avatarUrl}, signature = #{signature}, status = #{status}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int update(User user);

    /**
     * 更新用户在线状态
     */
    @Update("UPDATE users SET status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status, @Param("updateTime") Long updateTime);

    /**
     * 删除用户
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    int delete(Long id);
}